<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:aws="http://webservices.amazon.com/AWSECommerceService/2005-10-05" exclude-result-prefixes="aws">

	<xsl:output method="text"/>

<!-- +- -->
<!-- | Base Template Match, General JSON Format -->
<!-- +- -->
	<xsl:template match="/">
	    <xsl:variable name="query"><xsl:value-of select="/aws:ItemSearchResponse/aws:Items/aws:Request/aws:ItemSearchRequest/aws:SearchIndex"/>_<xsl:value-of select="/aws:ItemSearchResponse/aws:Items/aws:Request/aws:ItemSearchRequest/aws:Keywords"/>_1</xsl:variable>

	
	    AWSearch.results["<xsl:value-of select="$query"/>
		<xsl:text>"] = { total : </xsl:text><xsl:value-of select="aws:ItemSearchResponse/aws:Items/aws:TotalResults"/>
		<xsl:text>,asid : "</xsl:text><xsl:value-of select="aws:ItemSearchResponse/aws:OperationRequest/aws:Arguments/aws:Argument[@Name = 'AssociateTag']/@Value"/>
		<xsl:text>" ,items:[</xsl:text>
		<xsl:apply-templates/>
		<xsl:text>
		"end"
 ]
};
if(AWSearch.callbacks["</xsl:text><xsl:value-of select="$query"/><xsl:text>"]){
 AWSearch.callbacks["</xsl:text><xsl:value-of select="$query"/><xsl:text>"](AWSearch.results["</xsl:text><xsl:value-of select="$query"/><xsl:text>"])
}</xsl:text>
	</xsl:template>
	
	<xsl:template match="aws:RequestId"/>
	<xsl:template match="aws:RequestProcessingTime"/>
	<xsl:template match="aws:Items">
		<xsl:apply-templates select="aws:Item"/>
	</xsl:template>
	
<!-- +- -->
<!-- | Fetch ASIN, URL, Title, Price -->
<!-- +- -->	
	<xsl:template match="aws:Item">
		<xsl:text> 
{</xsl:text>
		<xsl:text>asin:"</xsl:text><xsl:value-of select="aws:ASIN"/><xsl:text>",</xsl:text>
		   <xsl:variable name="btitle">
                <xsl:value-of select="aws:ItemAttributes/aws:Title"/>
           </xsl:variable>
           <xsl:variable name="Remove">",'</xsl:variable> 
		<xsl:text>title:"</xsl:text> <xsl:value-of select="translate($btitle,$Remove,'')"></xsl:value-of><xsl:text>",</xsl:text>
		<xsl:text>url:"</xsl:text><xsl:value-of select="aws:DetailPageURL"/><xsl:text>",</xsl:text>
		<xsl:text>author:"</xsl:text> <xsl:value-of select="aws:ItemAttributes/aws:Author"/><xsl:text>",</xsl:text>
		<xsl:text>manufacturer:"</xsl:text><xsl:value-of select="aws:ItemAttributes/aws:Manufacturer"/><xsl:text>"</xsl:text>
		<xsl:text>}, </xsl:text>
	</xsl:template>

<!-- +- -->
<!-- | Title Template, used to strip out quotation marks (which would break the javascript) -->
<!-- +- -->	

<!-- +- -->
<!-- | Search-and-Replace Template, swaps one string (target) with another (replacement) -->
<!-- +- -->	
	
</xsl:stylesheet>
